package com.mayisoftware.mayioa.business.flow.impl.controller;

//import com.mayisoftware.mayioa.platform.impl.web.base.BaseController;
import com.mayisoftware.mayioa.business.flow.api.domain.BaseFlowInfo;
import com.mayisoftware.mayioa.business.flow.api.service.IFlowBaseService;
import com.mayisoftware.mayioa.common.api.base.AjaxResult;
import com.mayisoftware.mayioa.common.impl.utils.bean.ReflectionUtil;
import com.mayisoftware.mayioa.platform.api.domain.SysUser;
import com.mayisoftware.mayioa.platform.impl.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.flowable.bpmn.model.BpmnModel;
import org.flowable.engine.*;
import org.flowable.engine.history.HistoricActivityInstance;
import org.flowable.engine.runtime.Execution;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.image.ProcessDiagramGenerator;
import org.flowable.task.api.Task;
import org.flowable.variable.api.history.HistoricVariableInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.RepositoryService;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/flow/mgr")
public class FlowBaseController {
    private String prefix = "flow/mgr";

    @Autowired
    private IFlowBaseService flowBaseService;


    /**
     * 批准
     *
     */
    @RequiresPermissions("flow:mgr:apply")
    @RequestMapping(value = "apply")
    @ResponseBody
    public AjaxResult apply(String taskId,String remark) {
        boolean bok =   flowBaseService.apply(taskId,remark);
        if (bok){
            return AjaxResult.success("通过成功");
        }
        else {
            return AjaxResult.error("通过发生错误");
        }
    }

    /**
     * 拒绝
     */
    @ResponseBody
    @RequiresPermissions("flow:mgr:reject")
    @RequestMapping(value = "reject")
    public AjaxResult reject(String taskId, String remark) {
         boolean bok = flowBaseService.reject(taskId,remark);
         if (bok){
             return AjaxResult.success("驳回成功");
         }
         else {
             return AjaxResult.error("驳回发生错误");
         }
    }

    /**
     * 生成流程图
     *
     */
    @RequiresPermissions("flow:mgr:processDiagram")
    @RequestMapping(value = "processDiagram")
    public void genProcessDiagram(HttpServletResponse httpServletResponse, String processId) throws Exception {
        flowBaseService.genProcessDiagram(httpServletResponse,processId);
    }


    @RequiresPermissions("flow:mgr:processDiagram")
    @RequestMapping("/list/process")
    @ResponseBody
    public List<BaseFlowInfo> listByProcessId(String processId)
    {
        List<BaseFlowInfo> baseFlowInfos = new ArrayList<>();
        List<HistoricActivityInstance> historicActivityInstances = flowBaseService.queryProcessActivityInfos(processId);

        for (HistoricActivityInstance historicActivityInstance:historicActivityInstances){
            BaseFlowInfo baseFlowInfo =new BaseFlowInfo();

            String flowKey =  historicActivityInstance.getActivityId();

            if (historicActivityInstance.getActivityType().equalsIgnoreCase("exclusiveGateway")){
                continue;
            }

            List<HistoricVariableInstance> historicVariableInstances = flowBaseService.queryProcessVariableInfos(processId);

            boolean hasVar =false;
            for (HistoricVariableInstance historicVariableInstance:historicVariableInstances){

                String varName = historicVariableInstance.getVariableName();

                if (varName.startsWith(flowKey+"|") ){
                    String[] vars = varName.split("\\|",-1);
                    if (vars.length != 2){
                        continue;
                    }
                    hasVar = true;
                    ReflectionUtil.setFieldValue(baseFlowInfo,vars[1],historicVariableInstance.getValue());
                }
            }

            if (hasVar){
                baseFlowInfos.add(baseFlowInfo);
            }
        }

        return baseFlowInfos;
    }

}
